package main

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func main() {

	engine := gin.Default()

	engine.GET("/hello", RequestInfos(), func(context *gin.Context) {
		context.JSON(http.StatusOK, map[string]interface{}{
			"code": 1,
			"data": "查询",
		})
		context.Writer.WriteString("\n")
	})

	if err := engine.Run(":8080"); err != nil {
		log.Fatal(err.Error())
	}
}

// 自定义打印相关信息的中间件
func RequestInfos() gin.HandlerFunc {
	return func(context *gin.Context) {
		context.Writer.WriteString("111\n")
		context.Writer.WriteString(context.FullPath() + "\n")

		context.Next()

		context.Writer.WriteString("2222\n")
	}
}